<template>
  <div>
    <Searchbar>
      <el-input @change="getList" v-model="search.class_id" placeholder="请输入班级ID"></el-input>
      <el-autocomplete
        class="inline-input"
        v-model="selectedClass"
        :fetch-suggestions="queryClass"
        placeholder="请输入班级名称"
        @select="selectClass"
      ></el-autocomplete>
      <el-select @change="onGradeChange" v-model="search.grade" placeholder="请选择年级">
        <el-option v-for="item in grades" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <el-autocomplete
        class="inline-input"
        v-model="selectedSchool"
        :fetch-suggestions="querySchool"
        placeholder="请输入学校名称"
        @select="selectSchool"
      ></el-autocomplete>
      <el-autocomplete
        class="inline-input"
        v-model="selectedCollege"
        :fetch-suggestions="queryCollege"
        placeholder="请输入学院名称"
        @select="selectCollege"
      ></el-autocomplete>
      <el-autocomplete
        class="inline-input"
        v-model="selectedMajor"
        :fetch-suggestions="queryMajor"
        placeholder="请输入专业名称"
        @select="selectMajor"
      ></el-autocomplete>
      <el-select @change="onClass_statusChange" v-model="search.class_status" placeholder="请选择班级状态">
        <el-option
          v-for="item in class_status"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button class="search-btn" @click="getList" type="primary">查询</el-button>
      <el-button class="search-btn" @click="resetSearch" type="primary">重置</el-button>
    </Searchbar>
    <TableBar>
      <el-button @click="onShowCreate" type="primary" icon="el-icon-plus">新建</el-button>
      <el-button @click="onShowUpload" icon="el-icon-upload">批量上传</el-button>
      <el-button @click="onMulForbidden" icon="el-icon-lock">批量封停</el-button>
      <el-button @click="onMulEnable" icon="el-icon-unlock">批量启用</el-button>
    </TableBar>
    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column fixed="left" type="selection" width="55"></el-table-column>
      <el-table-column fixed="left" label="序号" type="index" width="55"></el-table-column>
      <el-table-column prop="class_id" label="班级ID" ></el-table-column>
      <el-table-column prop="class_name" label="班级名称" ></el-table-column>
      <el-table-column prop="grade" label="年级" ></el-table-column>
      <el-table-column prop="major_name" label="专业名称" ></el-table-column>
      <el-table-column prop="college_name" label="学院名称" ></el-table-column>
      <el-table-column prop="school_name" label="学校名称" ></el-table-column>
      <el-table-column prop="statusLabel" label="班级状态" ></el-table-column>
      <el-table-column prop="create_time" label="创建时间" ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="onShowDetail(scope.row)"
            size="small"
            class="text-button"
          >查看</el-button>
          <el-button @click="onShowEdit(scope.row)" size="small" class="text-default">编辑</el-button>
          <el-button
            @click="onForbidden(scope.row)"
            size="small"
            :class="scope.row.status == 1 ? 'text-warning' : 'text-button'"
          >{{scope.row.status == 1 ? '封停' : '启用'}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
       background
      layout="prev, pager, next, sizes, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-sizes="page_sizes"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <EditClass @afterSubmit="getList" @onClose="onHideEdit" :obj="editRow" :show="showEdit" />

    <MulUpload :temp="7" :type="1" @onClose="onHideUpload" :show="showUpload" />
  </div>
</template>
<script>
import EditClass from "@/modals/EditClass";
import MulUpload from "@/components/MulUpload";
import CreateFilter from "@/mixins/CreateFilter";
import showEditMixin from "@/mixins/showEditMixin";
import uploadMixin from "@/mixins/uploadMixin";
import pageMixin from "@/mixins/pageMixin";
import { classModel } from "@/api/admin_models";
import Searchbar from "@/components/Searchbar";
import TableBar from "@/components/TableBar";
import {Message} from 'element-ui';
export default {
  mixins: [CreateFilter, showEditMixin, uploadMixin, pageMixin],
  components: { EditClass, MulUpload, Searchbar, TableBar },
  data() {
    return {
      tableData: [],
      total: 0,
      search: {
        class_id: null,
        class_name: null,
        selectedclass_id: null,
        grade: null,
        selectedschool_id: null,
        selectedcollege_id: null,
        selectedmajor_id: null,
        class_status: null,
        page: 1,
        page_size: 10
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    resetSearch() {
      this.search = {
        class_id: null,
        class_name: null,
        selectedclass_id: null,
        grade: null,
        selectedschool_id: null,
        selectedcollege_id: null,
        selectedmajor_id: null,
        class_status: null,
        page: 1,
        page_size: 10
      }
      this.selectedSchool = ''
      this.selectedCollege = ''
      this.selectedMajor = ''
      this.getList();
    },
    doForbidden(ids, status, msg) {
      this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          classModel.batchBlock({
            classes: ids,
            status: status
          }, res => {
            this.$message({
              type: 'success',
              message: res.message 
            });
            this.getList();
          }, err => {console.log(err)})
        }).catch((err) => {
          console.error(err)
          this.$message({
            type: 'info',
            message: '已取消操作'
          });          
        });
    },
    onMulForbidden() {
      if(this.multipleSelection.length == 0) {
        return Message.warning('请选择要处理班级');
      }
      let ids = this.multipleSelection.map((v) => {
        return v.class_id;
      })
      this.doForbidden(ids, 2, '确定批量封禁班级？');
    },
    onMulEnable() {
      if(this.multipleSelection.length == 0) {
        return Message.warning('请选择要处理班级');
      }
      let ids = this.multipleSelection.map((v) => {
        return v.class_id;
      })
      this.doForbidden(ids, 1, '确定批量启用班级？');
    },
    onForbidden(row) {
      let ope = row.status == 1 ? '封禁' : '启用';
      let status = row.status == 1 ? 2 : 1;
      this.doForbidden([row.class_id], status, `确定${ope}班级${row.class_name}?`);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    onShowDetail(row) {
      this.$router.push('/class/' + row.class_id)
    },
    getList() {
      console.log("getList");
      classModel.list(
        this.search,
        res => {
          this.total = res.total_records;
          this.tableData = res.data.map(v => {
            v.statusLabel = this.classStatusToLabel(v.status);

            return v;
          });
        },
        err => {
          console.log(err);
        }
      );
    }
  }
};
</script>
<style lang="scss">
</style>